// pages/user/setUp/certification/second/second.js
import{
  getCity,
  getProvinces,
  bankList,
  bankBranchInfo,
  bankCertification
} from "../../../../../api.js"
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    SETTLE_BANKCARD: {
      url: "http://document.growthupup.com/add_pic.png",
      thumb: "",
      base_image: ""
    },
    HAND_BANKCARD: {
      url: "http://document.growthupup.com/add_pic.png",
      thumb: "",
      base_image: ""
    },
    province: [], //三联动
    city: [],
    area: [],
    index: -1,
    index2: -1,
    index3: -1,
    province_two: [], //二联动
    city_two: [],
    index_two: -1,
    index2_two: -1,
    totalBank_index: -1, //总行
    totalBank: [],
    branchBank: [], //支行
    branchBank_index: -1,
    is_licence: 0,
    headBankCode: '', //开户银行总行编码
    bankCity: '', //开户市
    bankCode: '', //开户银行支行编码
    bankProvince: '',
    inputvalue:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    getCity({
      token: app.globalData.token
    }).then(res => {  
      this.setData({
        province: res.data.data
      })
    })
    getProvinces({
      token: app.globalData.token
    }).then(res => { 
      this.setData({
        province_two: res.data.data
      })
    })
    bankList({
      token: app.globalData.token
    }).then(res => {
      console.log(res)
      this.setData({
        totalBank: res.data.data
      })
    })
  },
  //银行卡卡号方法
  focus(e){
    if (this.data.inputvalue !="请确保与上传银行卡上的一致"){
      return
    }
    this.setData({
      inputvalue:""
    })
  },
  input(e){
    this.setData({
      inputvalue:e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  bindProvince_two: function (e) {
    console.log()

    var that = this
    that.setData({
      index_two: e.detail.value,
      bankProvince: that.data.province_two[e.detail.value].value,
      city_two: that.data.province_two[e.detail.value].children,
      index2_two: -1,
      totalBank_index: -1,
      branchBank: '',
      branchBank_index: -1
    })
    console.log(this.data.bankProvince)
    console.log(this.data.city_two)
  },
  bindCity_two: function (e) {
    var that = this
    that.setData({
      index2_two: e.detail.value,
      bankCity: that.data.city_two[e.detail.value].value,
      totalBank_index: -1,
      branchBank: '',
      branchBank_index: -1
    })
  },
  //总行
  totalBank: function (e) {
    var that = this
    if (that.data.index_two == -1 || that.data.index2_two == -1) {
      wx.showToast({
        title: '请先选择开户地址',
        duration: 2000,
        icon: 'none'
      })
      return;
    }
    var textnum = that.data.totalBank[e.detail.value].name + '股份有限公司'
    that.setData({
      totalBank_index: e.detail.value,
      headBankCode: that.data.totalBank[e.detail.value].code,
      branchBank_index: -1
    })
    bankBranchInfo({
      token: app.globalData.token,
      headBankCode: that.data.headBankCode,
      provinceCode: that.data.bankProvince,
      cityCode: that.data.bankCity
    }).then(res => {
      if (res.data.code == 666) {
        console.log(res)
        // wx.hideLoading()
        console.log(res.data.data)
        if (res.data.data == '') {
          that.setData({
            branchBank: [{ id: '', name: '暂无数据' }],
            branchBank_index: 0
          })
        } else {
          for (var i = 0; i < res.data.data.length; i++) {
            res.data.data[i].name = res.data.data[i].name.replace(textnum, '')
          }
          that.setData({
            branchBank: res.data.data
          })
        }
      } else if (res.data.code == 111111) {
        wx.showToast({
          title: '登录失效，请重新登录',
          icon: 'none',
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '/pages/user/login/login',
          })
        }, 1500)
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
  },
  //获取省份
  bindProvince: function (e) {
    console.log(e)
    var that = this
    that.setData({
      index: e.detail.value,
      merProvince: that.data.province[e.detail.value].value,
      city: that.data.province[e.detail.value].children,
      index2: -1,
      index3: -1,
      area: []
    })
  },
  bindCity: function (e) {
    var that = this
    that.setData({
      index2: e.detail.value,
      merCity: that.data.city[e.detail.value].value,
      area: that.data.city[e.detail.value].children,
      index3: -1
    })
  },
  bindProvince: function (e) {
    console.log(e)
    var that = this
    that.setData({
      index: e.detail.value,
      merProvince: that.data.province[e.detail.value].value,
      city: that.data.province[e.detail.value].children,
      index2: -1,
      index3: -1,
      area: []
    })
  },
  //获取市
  bindCity: function (e) {
    var that = this
    that.setData({
      index2: e.detail.value,
      merCity: that.data.city[e.detail.value].value,
      area: that.data.city[e.detail.value].children,
      index3: -1
    })
  },
  // 获取银行信息
  branchBank: function (e) {
    var that = this
    console.log(that.data.branchBank[e.detail.value].id,)
    that.setData({
      branchBank_index: e.detail.value,
      bankCode: that.data.branchBank[e.detail.value].id,
    })
  },
  //上传银行卡图片
  SETTLE_BANKCARD(e) {
    var that = this
    wx.chooseImage({
      count: 1,
      success: function (res) {
        wx.showLoading({
          title: '正在上传图片',
          icon: 'loading'
        })
        var url = res.tempFilePaths;
        wx.uploadFile({
          url: app.globalData.imgurl,
          filePath: url[0],
          name: 'imgfile',
          formData: {
            'token': app.globalData.token
          },
          success: function (res) {


            var jsonStr = res.data;

            jsonStr = jsonStr.replace(" ", "");


            if (typeof jsonStr != 'object') {
              jsonStr = jsonStr.replace(/\ufeff/g, "");
              var jj = JSON.parse(jsonStr);
            }
            console.log(jj)
            if (jj.code == 666) {
              wx.hideLoading();
              that.setData({
              })
              that.data.SETTLE_BANKCARD.url = jj.data.url
              that.data.SETTLE_BANKCARD.thumb = jj.data.thumb
              that.data.SETTLE_BANKCARD._file = jj.data.base_image
              that.setData({
                SETTLE_BANKCARD: that.data.SETTLE_BANKCARD
              })
            } else if (jj.code == 111111) {
              wx.showToast({
                title: '登录失效，请重新登录',
                icon: 'none',
              })
              setTimeout(function () {
                wx.reLaunch({
                  url: '/pages/user/login/login',
                })
              }, 1500)
            } else {
              wx.showToast({
                title: jj.msg,
                icon: 'none'
              })
            }
          }
        })
      },
    })
  },
  //手持银行卡照片
  HAND_BANKCARD(e) {
    var that = this
    wx.chooseImage({
      count: 1,
      success: function (res) {
        wx.showLoading({
          title: '正在上传图片',
          icon: 'loading'
        })
        var url = res.tempFilePaths;
        wx.uploadFile({
          url: app.globalData.imgurl,
          filePath: url[0],
          name: 'imgfile',
          formData: {
            'token': app.globalData.token
          },
          success: function (res) {


            var jsonStr = res.data;

            jsonStr = jsonStr.replace(" ", "");


            if (typeof jsonStr != 'object') {
              jsonStr = jsonStr.replace(/\ufeff/g, "");
              var jj = JSON.parse(jsonStr);
            }
            console.log(jj)
            if (jj.code == 666) {
              wx.hideLoading();
              that.setData({
              })
              that.data.HAND_BANKCARD.url = jj.data.url
              that.data.HAND_BANKCARD.thumb = jj.data.thumb
              that.data.HAND_BANKCARD._file = jj.data.base_image
              that.setData({
                HAND_BANKCARD: that.data.HAND_BANKCARD
              })
            } else if (jj.code == 111111) {
              wx.showToast({
                title: '登录失效，请重新登录',
                icon: 'none',
              })
              setTimeout(function () {
                wx.reLaunch({
                  url: '/pages/user/login/login',
                })
              }, 1500)
            } else {
              wx.showToast({
                title: jj.msg,
                icon: 'none'
              })
            }
          }
        })
      },
    })
  },
  //去提交所填信息
  gonext(){
    if (this.data.SETTLE_BANKCARD.thumb == "" || this.data.HAND_BANKCARD.thumb == "" || this.data.inputvalue == "请确保与上传银行卡上的一致" || this.data.bankProvince == "" || this.data.bankCity == "" || this.data.headBankCode == "" || this.data.bankCode == ""){
      wx.showToast({
        title: '请按规定上传资料',
        icon:"none"
      })
        return
    }
    bankCertification({
      token: app.globalData.token,
      cardNo:this.data.inputvalue,
      bankProvince: this.data.bankProvince,
      bankCity: this.data.bankCity,
      headBankCode: this.data.headBankCode,
      bankCode: this.data.bankCode,
      fileInfo:{
        SETTLE_BANKCARD: this.data.SETTLE_BANKCARD,
        HAND_BANKCARD: this.data.HAND_BANKCARD
      }
    }).then(res=>{
      if(res.data.code===666){
        wx.showToast({
          title: '上传资料成功，正在为您跳转',
        })
        setTimeout(()=>{
          wx.reLaunch({
            url: '/pages/user/user',
          })
        },1000)
        
      }else{
        wx.showToast({
          title: '资料上传失败，请确认输入无误',
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  bank(e){

  }

})